1379A - Acacius and String - CodeForces Solution


brute force implementation strings *1500

Please click on ads to support us..

Python Code:

T = "abacaba"

def count(s):
    i = 0
    j = 7
    c = 0
    while i < len(s) and j >= 0:
        if s[i:j] == "abacaba":
            c += 1
            i+=1
            j+=1
        else:
            i+=1
            j+=1
    return c

def solve():
    n = int(input())
    s = input()
    F = False

    if count(s) == 1:
        F = True
        print("Yes")
        sc = s.replace('?', 'd')
        print(sc)
        return

    for i in range(n - len(T) + 1):
        sc = list(s)
        ok = True
        for j in range(len(T)):
            if sc[i + j] != '?' and sc[i + j] != T[j]:
                ok = False
                break
            sc[i + j] = T[j]

        if ok and count(''.join(sc)) == 1:
            sc = ''.join(sc)
            sc = sc.replace('?', 'd')
            F = True
            print("Yes")
            print(sc)
            return

    if not F:
        print("No")
        return

t = int(input())
for _ in range(t):
    solve()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(0)
#define endl '\n'
#define int long long
#define ar array<int, 2>
#define arr array<int, 3>
const int N = 2e5 + 5, M = 2 * N;
const int inf = 0x3f3f3f3f;
int mod = 998244353; //1e9+7;
int t, n, m, k;
string a = "abacaba";
signed main()
{
    ios;
#ifdef DEBUG
    freopen("../1.in", "r", stdin);
#endif
    //  核心技巧在于那个tmp的 再验证。。的手法。。
    // 就是匹配这个string  abacaba
    // 问 是否能实现当且仅当 匹配一次。。
    // 刚开始还想到dfs
    // 实际上只要从左往右 一截一截的去匹配验证就可以了
    // 1 匹配。2 验证是否能够唯一。
    cin >> t;
    while (t--)
    {
        cin >> n;
        string s;
        cin >> s;
        string ans;
        for (int i = 0; i <= n - 7; ++i)
        {
            string tmp = s;
            bool f = 1;
            for (int j = 0; j < 7; ++j)
            {
                if (tmp[i + j] != '?' && tmp[i + j] != a[j])
                {
                    f = 0;
                    break;
                }
                else
                    tmp[i + j] = a[j];
            }
            if (!f)
                continue;
            for (int i = 0; i <n; ++i)
                if (tmp[i] == '?')
                    tmp[i] = 'z';
            for (int j = 0; j <= n - 7; ++j)
                if (i != j && tmp.substr(j, 7) == a)
                {
                    f = 0;
                    break;
                }
            if (f)
            {
                ans = tmp;
                break;
            }
        }

        cout << (ans.size() ? "YES" : "NO") << endl;
        if (ans.size())
            cout << ans << endl;
    }
};


Comments

Submit
0 Comments
More Questions

102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort